package com.buss.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dao.UserDao;
import com.buss.core.exception.ServiceException;
import com.buss.core.model.User;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	private static final long serialVersionUID = 1L;

	@Autowired
	private UserDao userDao;
	
	public void deleteUser(Long[] userIds) {
		userDao.deleteUser(userIds);
		
	}

	public void editUser(User adminUser) {
		userDao.editUser(adminUser);
		
	}

	public void editUserPass(Long userId,String password){
		userDao.editUserPass(userId, password);
	}
	
	public void editOrganTransfer(Long[] userId,Long organId){
		userDao.editOrganTransfer(userId, organId);
	}
	
	
	public void editRoleToUserId(Long userId,Long[] roleIds){
		userDao.editRoleToUserId(userId, roleIds);
	}
	
	public User loadUser(Long userId) {		
		User user = userDao.loadUser(userId);
		return user;
	}
	
	public User loadUser(String companyId,String userId){
		User user = userDao.loadUser(companyId, userId);
		return user;
	}

	public ResultList<User> queryUser(String companyId, Long organId,String userName, Boolean disabled,
			Paginalbe page) {
		return userDao.queryUser(companyId,organId,userName, disabled, page);
	}

	/**
	 * 根据单位获取业务员信息
	 * @param companyId
	 * @param isSalesMan
	 * @return
	 */
	public ResultList<User> queryUserBySales(String companyId,Boolean isSalesMan){
		return userDao.queryUserBySales(companyId, isSalesMan);
	}
	
	public ResultList<User> queryUserByRoleId(String companyId, Long roleId,String userName,
			Boolean disabled,Paginalbe page){
		return userDao.queryUserByRoleId(companyId,roleId, userName, disabled, page);
	}
	
	public void saveUser(User adminUser) throws ServiceException {
		if(loadUser(adminUser.getCompanyId(), adminUser.getUserId()) != null){
			throw new ServiceException("用户编码已经存在，请核实!");
		}
		userDao.saveUser(adminUser);		
	}

	public void updateUsersRole(Long[] userIds,Long roleId){
		userDao.updateUsersRole(userIds, roleId);
	}
	
	public void removeUsersRole(Long[] userIds,Long roleId){
		userDao.removeUsersRole(userIds, roleId);
	}
}
